//list

var times = 0;//加载次数
var loaded = true;
var category = $("#category").val();
(function(){
	
	init();
	/*
	var asideList = $("#asideList").find(".js-tab__item");

	asideList.on("click", function(){
    	loaded = true;
    	times = 0;
        var $this = $(this);
        var index = $this.closest("li").index();
        if(index==0)
        {
        	category="0";
        }
        else if(index==1)
        {
        	category="1";
        }
        else if(index==2)
        {
        	category="2";
        }
        else if(index==3)
        {
        	category="3";
        }
        $("#content").empty();
        LoadData();
    });
	*/
    $(".scrollLoading").scrollLoading();    
    $(window).scroll(LoadData); 
}());

function init()
{
	//init
	$.ajax({
        type: "GET",
        dataType: "json",
        url: "lis",
        data: "category="+category+"&start="+times+"&length="+5,
        success: function(data){
        	var returnFlag = data.returnFlag;
            var list = data.list;
            
            if(returnFlag){
                times += 1;
                if(list.length === 0){
                    loaded = false;
                 }
                 else{
                    $.each(list, function(index, item){
                    	var path=$("#contextPath").val()+"/res/detail?id="+item.id;
                        $("#content").append("<li>"+
        						                "<div class='content-list__img'>"+
        							                "<a href='#'><img src='"+item.carouselImage+"' width='243' height='161' alt=''></a>"+
        							            "</div>"+
        							            "<div class='content-list__text'>"+
        							                "<h4>"+item.title+"</h4>"+
        							                "<p>"+item.abstractInfor+"</p>"+
        							                "<a class='look-detail' href='"+path+"'>查看详情 &gt;&gt;</a>"+
        							            "</div></li>");
                    });                    
                 }
            }
        },
        error: function(msg){
            console.log("加载失败！");
        }
      });
}

function LoadData(){
    var scrollTop = $(this).scrollTop();
    var scrollHeight = $(document).height();
    var windowHeight = $(this).height();

    $.ajax({
        type: "GET",
        dataType: "json",
        url: "lis",
        data: "category="+category+"&start="+times+"&length="+5,
        success: function(data){
        	
        	var returnFlag = data.returnFlag;
            var list = data.list;
            if(returnFlag){
              if(times==0 || loaded && (parseFloat(scrollTop + windowHeight) >= scrollHeight)){
                times += 1;
                if(list.length === 0){
                    loaded = false;
                 }
                 else{
                    $.each(list, function(index, item){      
                    	var path=$("#contextPath").val()+"/res/detail?id="+item.id;
                        $("#content").append("<li>"+
        						                "<div class='content-list__img'>"+
        							                "<a href='#'><img src='"+item.carouselImage+"' width='243' height='161' alt=''></a>"+
        							            "</div>"+
        							            "<div class='content-list__text'>"+
        							                "<h4>"+item.title+"</h4>"+
        							                "<p>"+item.abstractInfor+"</p>"+
        							                "<a class='look-detail' href='"+path+"'>查看详情 &gt;&gt;</a>"+
        							            "</div></li>");
                    });                    
                 }
              }
            }
        },
        error: function(msg){
            console.log("加载失败！");
        }
      });
}